﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace RM.Core
{
    public class RmEngine:IEngine
    {
        #region Fields

        private ContainerManager _containerManager;

        #endregion

        #region Ctor

        /// <summary>
		/// Creates an instance of the content engine using default settings and configuration.
		/// </summary>
		public RmEngine() 
            : this(new ContainerConfigurer())
		{
		}

        public RmEngine(ContainerConfigurer configurer)
		{
            var config = ConfigurationManager.GetSection("RmConfig") as RmConfig;
            InitializeContainer(configurer, config);
		}
        
        #endregion

        #region Utilities
        
        private void InitializeContainer(ContainerConfigurer configurer, RmConfig config)
        {
            var builder = new ContainerBuilder();
            _containerManager = new ContainerManager(builder.Build());
            configurer.Configure(this, _containerManager, config);
        }

        #endregion

        #region Methods

        public T Resolve<T>() where T : class
		{
            return ContainerManager.Resolve<T>();
		}

        public object Resolve(Type type)
        {
            return ContainerManager.Resolve(type);
        }
        
        public T[] ResolveAll<T>()
        {
            return ContainerManager.ResolveAll<T>();
        }

		#endregion

        #region Properties

        public ContainerManager ContainerManager
        {
            get { return _containerManager; }
        }

        #endregion
    }
}
